<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业全称" prop="supplierUnitName">
        <el-input
          v-model="queryParams.supplierUnitName"
          placeholder="请输入企业全称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="personName">
        <el-input
          v-model="queryParams.personName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="unitType">
        <el-select v-model="queryParams.unitType" style="width: 150px;" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.order_unit_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="法人姓名" prop="comContactName">
        <el-input
          v-model="queryParams.comContactName"
          placeholder="请输入法人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人身份证" prop="comContactIdCard">
        <el-input
          v-model="queryParams.comContactIdCard"
          placeholder="请输入法人身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人地址" prop="comContactAddr">
        <el-input
          v-model="queryParams.comContactAddr"
          placeholder="请输入法人地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="创建时间" prop="addTime">
        <el-date-picker clearable
          v-model="queryParams.addTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>-->
<!--      <el-form-item label="统一社会信用代码" prop="socialCreditCode">
        <el-input
          v-model="queryParams.socialCreditCode"
          placeholder="请输入统一社会信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="注册时间" prop="regTime">
        <el-date-picker clearable
          v-model="queryParams.regTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择注册时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="企业地址" prop="comAddress">
        <el-input
          v-model="queryParams.comAddress"
          placeholder="请输入企业地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业邮箱" prop="comEmail">
        <el-input
          v-model="queryParams.comEmail"
          placeholder="请输入企业邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册资金" prop="regAmount">
        <el-input
          v-model="queryParams.regAmount"
          placeholder="请输入注册资金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信商户号" prop="merchantId">
        <el-input
          v-model="queryParams.merchantId"
          placeholder="请输入微信商户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行开户支行" prop="openingBank">
        <el-input
          v-model="queryParams.openingBank"
          placeholder="请输入银行开户支行"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账户" prop="bankAccount">
        <el-input
          v-model="queryParams.bankAccount"
          placeholder="请输入银行账户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工商营业执照" prop="compics">
        <el-input
          v-model="queryParams.compics"
          placeholder="请输入工商营业执照"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--  <el-form-item label="手机号" prop="personTel">
        <el-input
          v-model="queryParams.personTel"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="personEmail">
        <el-input
          v-model="queryParams.personEmail"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="personAddr">
        <el-input
          v-model="queryParams.personAddr"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递地址" prop="expressAddr">
        <el-input
          v-model="queryParams.expressAddr"
          placeholder="请输入快递地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supplerunit:vehsupplierunit:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supplerunit:vehsupplierunit:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['supplerunit:vehsupplierunit:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supplerunit:vehsupplierunit:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehsupplierunitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键id" align="center" prop="id" />-->
      <el-table-column label="企业全称" align="center" prop="supplierUnitName" />
      <el-table-column label="法人姓名" align="center" prop="comContactName" />
      <el-table-column label="法人身份证" align="center" prop="comContactIdCard" />
      <el-table-column label="法人地址" align="center" prop="comContactAddr" />
      <el-table-column label="创建时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="统一社会信用代码" align="center" prop="socialCreditCode" />-->
      <el-table-column label="注册时间" align="center" prop="regTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.regTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="企业地址" align="center" prop="comAddress" />
      <el-table-column label="企业邮箱" align="center" prop="comEmail" />
      <el-table-column label="注册资金" align="center" prop="regAmount" />
      <el-table-column label="微信商户号" align="center" prop="merchantId" />
      <el-table-column label="银行开户支行" align="center" prop="openingBank" />
      <el-table-column label="银行账户" align="center" prop="bankAccount" />
      <el-table-column label="工商营业执照" align="center" prop="compics" />-->
      <el-table-column label="姓名" align="center" prop="personName" />
      <el-table-column label="手机号" align="center" prop="personTel" />
      <el-table-column label="邮箱" align="center" prop="personEmail" />
      <el-table-column label="地址" align="center" prop="personAddr" />
<!--      <el-table-column label="快递地址" align="center" prop="expressAddr" />-->
      <el-table-column label="类型" align="center" prop="unitType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_unit_type" :value="scope.row.unitType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supplerunit:vehsupplierunit:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supplerunit:vehsupplierunit:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-input v-show="false" v-model="form.id"></el-input>
        <el-form-item label="类型" prop="unitType">
          <el-radio-group v-model="form.unitType" @change="handleChange">
            <el-radio
              v-for="dict in dict.type.order_unit_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="企业全称" prop="supplierUnitName" v-if="isShowCompany">
          <el-input v-model="form.supplierUnitName" placeholder="请输入企业全称" />
        </el-form-item>
        <el-form-item label="法人姓名" prop="comContactName" v-if="isShowCompany">
          <el-input v-model="form.comContactName" placeholder="请输入法人姓名" />
        </el-form-item>
        <el-form-item label="法人身份证" prop="comContactIdCard" v-if="isShowCompany">
          <el-input v-model="form.comContactIdCard" placeholder="请输入法人身份证" />
        </el-form-item>
        <el-form-item label="法人地址" prop="comContactAddr" v-if="isShowCompany">
          <el-input v-model="form.comContactAddr" placeholder="请输入法人地址" />
        </el-form-item>
<!--        <el-form-item label="创建时间" prop="addTime">
          <el-date-picker clearable
            v-model="form.addTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>-->
        <el-form-item label="统一社会信用代码" prop="socialCreditCode" v-if="isShowCompany">
          <el-input v-model="form.socialCreditCode" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="注册时间" prop="regTime" v-if="isShowCompany">
          <el-date-picker clearable
            v-model="form.regTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="企业地址" prop="comAddress" v-if="isShowCompany">
          <el-input v-model="form.comAddress" placeholder="请输入企业地址" />
        </el-form-item>
        <el-form-item label="企业邮箱" prop="comEmail" v-if="isShowCompany">
          <el-input v-model="form.comEmail" placeholder="请输入企业邮箱" />
        </el-form-item>
        <el-form-item label="注册资金" prop="regAmount" v-if="isShowCompany">
          <el-input v-model="form.regAmount" placeholder="请输入注册资金" />
        </el-form-item>
        <el-form-item label="微信商户号" prop="merchantId" v-if="isShowCompany">
          <el-input v-model="form.merchantId" placeholder="请输入微信商户号" />
        </el-form-item>
        <el-form-item label="银行开户支行" prop="openingBank" v-if="isShowCompany">
          <el-input v-model="form.openingBank" placeholder="请输入银行开户支行" />
        </el-form-item>
        <el-form-item label="银行账户" prop="bankAccount" v-if="isShowCompany">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账户" />
        </el-form-item>
        <el-form-item label="工商营业执照" prop="compics" v-if="isShowCompany">
          <input type="file" @change="handleFileUpload" multiple />
          <div>
            请上传最新的营业执照，格式要求:原件照片、扫描件或者加盖公章的复印件，支持,jpg
            jpeg .bmp .gifpng格式照片，大小不超过10M。
          </div>
        </el-form-item>
        <el-form-item label="姓名" prop="personName" v-if="isShowPerson">
          <el-input v-model="form.personName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="personTel" v-if="isShowPerson">
          <el-input v-model="form.personTel" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="personEmail" v-if="isShowPerson">
          <el-input v-model="form.personEmail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="地址" prop="personAddr" v-if="isShowPerson">
          <el-input v-model="form.personAddr" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="快递地址" prop="expressAddr" v-if="isShowPerson">
          <el-form label-width="auto" :model="infomation" :inline="true">
            <div v-for="(item, index) in infomation.infolist" :key="index">
              <el-input v-model="item.id" v-show="false"/>
              <el-form-item label="姓名">
                <el-input v-model="item.name" placeholder="请输入姓名" style="width:150px"/>
              </el-form-item>
              <el-form-item label="快递地址">
                <el-input v-model="item.expressAddr" placeholder="请输入快递地址" style="width:200px"/>
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="item.telephone" placeholder="请输入电话" style="width:200px"/>
              </el-form-item>

              <el-form-item>
                <el-button @click="addItem()"> + </el-button>
                <el-button
                  @click="deleteItem(index)"
                >
                  –
                </el-button>
              </el-form-item>
            </div>
          </el-form>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVehsupplierunit, getVehsupplierunit, delVehsupplierunit, addVehsupplierunit, updateVehsupplierunit } from "@/api/supplerunit/vehsupplierunit";
import {systemuseruploadfiles} from "@/api/system/user";
import {addExpinfo, delExpinfo, listExpinfo, updateExpinfo} from "@/api/expressinfo/expinfo";
import store from "@/store";
export default {
  name: "Vehsupplierunit",
  dicts: ['order_unit_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      isShowPerson: true,
      isShowCompany: false,
      // 总条数
      total: 0,
      // 供应商单位表格数据
      vehsupplierunitList: [],
      // 快递地址表格数据
      expinfoList: [],
      infomation: {
        infolist: [{ id: "",name: "", expressAddr: "", telephone: "" }],
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierUnitName: null,
        comContactName: null,
        comContactIdCard: null,
        comContactAddr: null,
        addTime: null,
        socialCreditCode: null,
        regTime: null,
        comAddress: null,
        comEmail: null,
        regAmount: null,
        merchantId: null,
        openingBank: null,
        bankAccount: null,
        compics: null,
        personName: null,
        personTel: null,
        personEmail: null,
        personAddr: null,
        expressAddr: null,
        unitType: null
      },
      // 表单参数
      form: {},
      expressForm: {},
      queryExpressForm: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供应商单位列表 */
    getList() {
      this.loading = true;
      listVehsupplierunit(this.queryParams).then(response => {
        this.vehsupplierunitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    addItem() {
      let that = this;
      var index;
      for(var i=0;i<this.infomation.infolist.length;i++){
        this.expressForm.name=that.infomation.infolist[i].name;
        this.expressForm.expressAddr=that.infomation.infolist[i].expressAddr;
        this.expressForm.telephone=that.infomation.infolist[i].telephone;
        index=i;
      }
      this.expressForm.userId=store.getters.userId;
      this.expressForm.expressType=1;
      this.expressForm.id=null;
      if(this.form.id!=null){
        this.expressForm.supplierUnitId=this.form.id;
      }
      addExpinfo(this.expressForm).then(response => {
        this.$modal.msgSuccess("新增成功")
        that.infomation.infolist[index].id=response.expressId;
      });
      that.infomation.infolist.push({
        id: "",
        name: "",
        expressAddr: "",
        telephone: "",
      });
    },

    deleteItem(i) {
      if (this.infomation.infolist.length <= 1) {
        return false;
      }
      console.log(this.infomation.infolist[i].name+this.infomation.infolist[i].expressAddr+this.infomation.infolist[i].telephone);
      if(this.infomation.infolist[i].id!='') {
        delExpinfo(this.infomation.infolist[i].id).then(response => {
          this.$modal.msgSuccess("删除成功");
        })
      }
      this.infomation.infolist.splice(i, 1);


    },
    handleFileUpload(event) {
      // 获取用户选择的文件
      console.log(event);
      // 创建 FormData 对象
      // 将选定的文件存储到 selectedFiles 数组中
      let selectedFiles = Array.from(event.target.files);

      // 将 selectedFiles 数组中的文件上传到后端服务器
      const formData = new FormData();

      if (selectedFiles) {
        selectedFiles.forEach((file) => {
          formData.append("files", file);
        });
      }

      console.log(formData);
      // return;
      // 发送 POST 请求上传图片
      systemuseruploadfiles(formData).then((res) => {
        console.log(res);
        this.form.compics = res.urls;
      });
    },
    handleChange(value) {
      if(value==0){
        this.isShowPerson=true;
        this.isShowCompany=false;
      }else{
        this.isShowPerson=false;
        this.isShowCompany=true;
      }
      if(this.form.id==null || this.form.id==''){
        this.form.supplierUnitName='';
        this.form.comContactName='';
        this.form.comContactIdCard='';
        this.form.comContactAddr='';
        this.form.socialCreditCode='';
        this.form.regTime='';
        this.form.comAddress='';
        this.form.comEmail='';
        this.form.regAmount='';
        this.form.merchantId='';
        this.form.openingBank='';
        this.form.bankAccount='';
        this.form.personName='';
        this.form.personTel='';
        this.form.personEmail='';
        this.form.personAddr='';
        this.form.expressAddr='';
        this.form.compics='';
      }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supplierUnitName: null,
        comContactName: null,
        comContactIdCard: null,
        comContactAddr: null,
        addTime: null,
        socialCreditCode: null,
        regTime: null,
        comAddress: null,
        comEmail: null,
        regAmount: null,
        merchantId: null,
        openingBank: null,
        bankAccount: null,
        compics: null,
        personName: null,
        personTel: null,
        personEmail: null,
        personAddr: null,
        expressAddr: null,
        unitType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.unitType=0;
      this.open = true;
      this.infomation.infolist=[];
      this.infomation.infolist.push({
        id: "",
        name: "",
        expressAddr: "",
        telephone: "",
      });
      this.title = "添加供应商单位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      if(row.unitType==0){
        this.isShowPerson=true;
        this.isShowCompany=false;

      }else{
        this.isShowPerson=false;
        this.isShowCompany=true;
      }
      const id = row.id || this.ids
      getVehsupplierunit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商单位";
      });
      this.infomation.infolist=[];
      this.queryExpressForm.supplierUnitId=id;
      listExpinfo(this.queryExpressForm).then(response => {
        this.expinfoList = response.rows;
        if(this.expinfoList.length>0) {
          for (var i = 0; i < this.expinfoList.length; i++) {
            this.infomation.infolist.push({
              id: this.expinfoList[i].id,
              name: this.expinfoList[i].name,
              expressAddr: this.expinfoList[i].expressAddr,
              telephone: this.expinfoList[i].telephone,
            });
          }
        }else{
          this.infomation.infolist.push({
            id: "",
            name: "",
            expressAddr: "",
            telephone: "",
          });
        }
        this.total = response.total;
        this.loading = false;
      });


    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVehsupplierunit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              for(var i=0;i<this.infomation.infolist.length;i++){
                if(this.infomation.infolist[i].name!='' || this.infomation.infolist[i].expressAddr!=''  || this.infomation.infolist[i].telephone!='') {
                  this.expressForm.supplierUnitId = this.form.id;
                  this.expressForm.id = this.infomation.infolist[i].id;
                  updateExpinfo(this.expressForm).then(response => {

                  });
                }
              }
              this.open = false;
              this.getList();
            });
          } else {
            addVehsupplierunit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              for(var i=0;i<this.infomation.infolist.length;i++){
                if(this.infomation.infolist[i].name!='' || this.infomation.infolist[i].expressAddr!=''  || this.infomation.infolist[i].telephone!=''){
                  this.expressForm.supplierUnitId=response.supplierUnitId;
                  this.expressForm.id=this.infomation.infolist[i].id;
                  updateExpinfo(this.expressForm).then(response => {

                  });
                }
              }
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据项？').then(function() {
        return delVehsupplierunit(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('supplerunit/vehsupplierunit/export', {
        ...this.queryParams
      }, `vehsupplierunit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
